package it.laypay.business;

import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * User: Aruna Rajapaksha
 */

public class BusinessDetailsPage extends BusinessPage {

    //Elements
    String pageTitle = "- Laypay business portal"; //TODO
    By displayName = By.id("DisplayName");
    By businessName = By.id("BusinessName");
    By australianBusinessNo = By.id("ABN");
    By accountID = By.id("AccountId");
    By businessType = By.id("BusinessTypeName");
    By title = By.id("Title");
    By firstName = By.id("FirstName");
    By middleName = By.id("MiddleName");
    By lastName = By.id("LastName");
    By email = By.id("Email");
    By password = By.id("Password");
    By confirmPassword = By.id("ConfirmPassword");
    By businessPhoneNo = By.id("BusinessPhoneNo");
    By mobileNo = By.id("MobileNo");
    By termsAndConditions = By.id("TermsAndConditions");
    By logoSmall = By.id("LogoSmall");
    By logoLarge = By.id("LogoLarge");
    By installmentType = By.id("InstallmentTypes");
    By paymentType = By.id("PaymentTypes");

    //TODO
    String expectedLabels = "DisplayName,BusinessName,AccountId,Title,BusinessTypeName,FirstName,LastName,Email," +
            "BusinessPhoneNo,MobileNo,ABN,TermsAndConditions,InstallmentTypes,PaymentTypes";

    protected void validateTypes(By by, String expectedTypes) throws Exception {

        List<String> expectedType = new LinkedList<String>(Arrays.asList(expectedTypes.split(DATA_DELIMITER)));
        List<String> actualType = new LinkedList<String>(new ArrayList<String>());
        List<String> tempExpectedType = new LinkedList<String>(new ArrayList<String>());

        List<WebElement> typeList = driver.findElements(by);
        for (WebElement webElement : typeList) {
            actualType.add(webElement.getText());
        }

        tempExpectedType.addAll(expectedType);
        expectedType.removeAll(actualType);
        actualType.removeAll(tempExpectedType);

        if (expectedType.size() > 0 || actualType.size() > 0) {
            verificationErrors.append("\nExpected Labels: ");
            verificationErrors.append(expectedLabels);
            verificationErrors.append("\nActual Labels: ");
            verificationErrors.append(actualType.toString());
        }
    }

    public void validateLabels() throws Exception {

        click(businessDetails);
        checkPageTitle(pageTitle);
        validateLabels(expectedLabels);
    }


    public void view(JSONObject data) throws Exception {

        click(businessDetails);
        checkPageTitle(pageTitle);

        readAndCompare(displayName, data.getString("DisplayName"));
        readAndCompare(businessName, data.getString("BusinessName"));
        readAndCompare(australianBusinessNo, data.getString("AustralianBusinessNo"));
        readAndCompare(accountID, data.getString("AccountID"));
        readAndCompare(businessType, data.getString("BusinessType"));
        readAndCompare(title, data.getString("Title"));
        readAndCompare(firstName, data.getString("FirstName"));
        //readAndCompare(middleName, data.getString("MiddleName"));//TODO not available
        readAndCompare(lastName, data.getString("LastName"));
        readAndCompare(email, data.getString("Email"));
        //readAndCompare(password, data.getString("Password"));//TODO note available. can ignore
        readAndCompare(businessPhoneNo, data.getString("BusinessPhoneNo"));
        readAndCompare(mobileNo, data.getString("MobileNo"));
        readAndCompare(termsAndConditions, data.getString("TermsAndConditions"));
        //readAndCompare(logoSmall, data.getString("LogoSmall"));//TODO not available
        //readAndCompare(logoLarge, data.getString("LogoLarge"));//TODO not available
        validateTypes(installmentType, data.getString("InstallmentTypes"));
        validateTypes(paymentType, data.getString("PaymentTypes"));

    }

}
